﻿using System;
using System.IO;
using System.Text;
using Aeon.Emulator.Dos;
using Aeon.Emulator.Video;

namespace Aeon.Emulator
{
    /// <summary>
    /// Provides Stream-based access to the emulated text console device.
    /// </summary>
    public sealed class ConsoleOutStream : Stream, IDeviceStream
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the ConsoleOutStream class.
        /// </summary>
        /// <param name="console">TextConsole instance where data is written.</param>
        internal ConsoleOutStream(TextConsole console)
        {
            if(console == null)
                throw new ArgumentNullException("console");

            this.console = console;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets a value indicating whether data can be read from the stream.
        /// </summary>
        public override bool CanRead
        {
            get { return false; }
        }
        /// <summary>
        /// Gets a value indicating whether seeking is supported on the stream.
        /// </summary>
        public override bool CanSeek
        {
            get { return false; }
        }
        /// <summary>
        /// Gets a value indicating whether data can be written to the stream.
        /// </summary>
        public override bool CanWrite
        {
            get { return true; }
        }
        /// <summary>
        /// Gets the length of the stream. This is undefined for ConsoleOutStream.
        /// </summary>
        public override long Length
        {
            get { return 0; }
        }
        /// <summary>
        /// Gets or sets the position in the stream. This is undefined for ConsoleOutStream.
        /// </summary>
        public override long Position
        {
            get
            {
                return 0;
            }
            set
            {
                throw new NotSupportedException();
            }
        }
        /// <summary>
        /// Gets information about the state of the device.
        /// </summary>
        public DosDeviceInfo DeviceInfo
        {
            get
            {
                return DosDeviceInfo.ConsoleOutputDevice | DosDeviceInfo.SpecialDevice | DosDeviceInfo.NotEndOfFile;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Flushes buffered data to the output device.
        /// </summary>
        public override void Flush()
        {
        }
        /// <summary>
        /// Reads data from the stream. This is not supported on ConsoleOutStream.
        /// </summary>
        /// <param name="buffer">Buffer into which data is read.</param>
        /// <param name="offset">Offset in buffer to start writing.</param>
        /// <param name="count">Number of bytes to read.</param>
        /// <returns>Number of bytes actually read.</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }
        /// <summary>
        /// Reads a byte from the stream. This is not supported on ConsoleOutStream.
        /// </summary>
        /// <returns>Byte read from the stream.</returns>
        public override int ReadByte()
        {
            throw new NotSupportedException();
        }
        /// <summary>
        /// Seeks to a new position in the stream. This is not supported on ConsoleOutStream.
        /// </summary>
        /// <param name="offset">Offset in stream to seek to.</param>
        /// <param name="origin">Stream seek origin.</param>
        /// <returns>New stream position.</returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            return offset;
        }
        /// <summary>
        /// Sets the length of the stream. This is not supported on ConsoleOutStream.
        /// </summary>
        /// <param name="value">New stream length.</param>
        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }
        /// <summary>
        /// Writes data to the stream.
        /// </summary>
        /// <param name="buffer">Buffer from which data is read.</param>
        /// <param name="offset">Offset in buffer to start reading.</param>
        /// <param name="count">Number of byte to write.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if(buffer == null)
                throw new ArgumentNullException("buffer");
            if(offset < 0 || offset >= buffer.Length)
                throw new ArgumentOutOfRangeException("offset");
            if(count < 0 || count > buffer.Length - offset)
                throw new ArgumentOutOfRangeException("count");

            string s = Encoding.ASCII.GetString(buffer, offset, count);
            this.console.Write(s);
        }
        /// <summary>
        /// Writes a string to the stream as ASCII bytes.
        /// </summary>
        /// <param name="s">String to write to the stream.</param>
        public void WriteString(string s)
        {
            if(s == null)
                throw new ArgumentNullException("s");

            this.console.Write(s);
        }
        /// <summary>
        /// Writes a byte to the stream.
        /// </summary>
        /// <param name="value">Byte to write to the stream.</param>
        public override void WriteByte(byte value)
        {
            this.console.Write(value);
        }
        /// <summary>
        /// Closes the stream.
        /// </summary>
        public override void Close()
        {
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Releases resources used by the stream.
        /// </summary>
        /// <param name="disposing">Value indicating whether the method was called from Dispose.</param>
        protected override void Dispose(bool disposing)
        {
            if(!disposing)
                base.Dispose(disposing);
        }
        #endregion

        #region Private Fields
        /// <summary>
        /// Console output device.
        /// </summary>
        private readonly TextConsole console;
        #endregion
    }
}
